<?php
$this->load->model("core/file");
class ModelQuanlykhoSanpham extends ModelCoreFile
{
	public function getItem($id, $where="")
	{
		$sql = "Select `qlksanpham`.*
									from `qlksanpham` 
									where id ='".$id."' ".$where;
		$query = $this->db->query($sql);
		return $query->row;
	}

	public function getSanPham($masanpham, $where="")
	{
		$sql = "Select `qlksanpham`.*
									from `qlksanpham` 
									where masanpham ='".$masanpham."' ".$where;
		$query = $this->db->query($sql);
		return $query->row;
	}

	public function getList($where="", $from=0, $to=0,$order="")
	{

		$sql = "Select `qlksanpham`.*
									from `qlksanpham` 
									where trangthai <> 'deleted' " . $where ;
		if($order == "")
			$sql .= " Order by tensanpham ASC";
		if($to > 0)
		{
			$sql .= " Limit ".$from.",".$to;
		}

		$query = $this->db->query($sql);
		return $query->rows;
	}

	public function insert($data)
	{
		$id= $this->db->escape(@$data['id']);
		$masanpham=$this->db->escape(@$data['masanpham']);
		$tensanpham=$this->db->escape(@$data['tensanpham']);
		$mavach=$this->db->escape(@$data['mavach']);
		$giacodinh= $this->string->toNumber($this->db->escape(@$data['giacodinh']));
		$sosanphamtrenlot= $this->string->toNumber($this->db->escape(@$data['sosanphamtrenlot']));
		$dongiabancai= $this->string->toNumber($this->db->escape(@$data['dongiabancai']));
		$dongiabanhop= $this->string->toNumber($this->db->escape(@$data['dongiabanhop']));
		$dongiabanthung= $this->string->toNumber($this->db->escape(@$data['dongiabanthung']));
		$dongiabanlot= $this->string->toNumber($this->db->escape(@$data['dongiabanlot']));
		$soluongton= $this->string->toNumber($this->db->escape(@$data['soluongton']));
		$donggoi=$this->string->toNumber($this->db->escape(@$data['donggoi']));
		$khuvuc=$this->string->toNumber($this->db->escape(@$data['khuvuc']));
		$phancap=$this->string->toNumber($this->db->escape(@$data['phancap']));
		$hienhanh=$this->string->toNumber($this->db->escape(@$data['hienhanh']));
		$ghichu=$this->db->escape(@$data['ghichu']);
		$manhom=$this->db->escape(@$data['manhom']);
		$loai=$this->db->escape(@$data['loai']);
		$madonvi=$this->db->escape(@$data['madonvi']);
		$trongluong=$this->string->toNumber($this->db->escape(@$data['trongluong']));
		$makho=$this->db->escape(@$data['makho']);
		$imageid=(int)@$data['imageid'];
		$imagepath=$this->db->escape(@$data['imagepath']);
		$field=array(

						'masanpham',
						'tensanpham',
						'mavach',
						'giacodinh',
						'sosanphamtrenlot',
						'dongiabancai',
						'dongiabanhop',
						'dongiabanthung',
						'dongiabanlot',
						'soluongton',
						'donggoi',
						'khuvuc',
						'phancap',
						'hienhanh',
						'ghichu',
						'loai',
						'manhom',
						'madonvi',
						'trongluong',
						'makho',
						'trangthai',
						'imageid',
						'imagepath'
						);
						$value=array(

						$masanpham,
						$tensanpham,
						$mavach,
						$giacodinh,
						$sosanphamtrenlot,
						$dongiabancai,
						$dongiabanhop,
						$dongiabanthung,
						$dongiabanlot,
						$soluongton,
						$donggoi,
						$khuvuc,
						$phancap,
						$hienhanh,
						$ghichu,
						$loai,
						$manhom,
						$madonvi,
						$trongluong,
						$makho,
						'active',
						$imageid,
						$imagepath
						);
						$this->db->insertData("qlksanpham",$field,$value);
						$log['tablename'] = "qlksanpham";
						$log['data'] = $data;
						$this->user->writeLog(json_encode($log));
						$this->updateFileTemp($imageid);
						return $id;
	}

	public function update($data)
	{

		$id= $this->db->escape(@$data['id']);
		$masanpham=$this->db->escape(@$data['masanpham']);
		$tensanpham=$this->db->escape(@$data['tensanpham']);
		$mavach=$this->db->escape(@$data['mavach']);
		$giacodinh= $this->string->toNumber($this->db->escape(@$data['giacodinh']));
		$sosanphamtrenlot= $this->string->toNumber($this->db->escape(@$data['sosanphamtrenlot']));
		$dongiabancai= $this->string->toNumber($this->db->escape(@$data['dongiabancai']));
		$dongiabanhop= $this->string->toNumber($this->db->escape(@$data['dongiabanhop']));
		$dongiabanthung= $this->string->toNumber($this->db->escape(@$data['dongiabanthung']));
		$dongiabanlot= $this->string->toNumber($this->db->escape(@$data['dongiabanlot']));
		$soluongton= $this->string->toNumber($this->db->escape(@$data['soluongton']));
		$donggoi=$this->string->toNumber($this->db->escape(@$data['donggoi']));
		$khuvuc=$this->string->toNumber($this->db->escape(@$data['khuvuc']));
		$phancap=$this->string->toNumber($this->db->escape(@$data['phancap']));
		$hienhanh=$this->string->toNumber($this->db->escape(@$data['hienhanh']));
		$ghichu=$this->db->escape(@$data['ghichu']);
		$manhom=$this->db->escape(@$data['manhom']);
		$loai=$this->db->escape(@$data['loai']);
		$madonvi=$this->db->escape(@$data['madonvi']);
		$trongluong=$this->string->toNumber($this->db->escape(@$data['trongluong']));
		$makho=$this->db->escape(@$data['makho']);
		$imageid=(int)@$data['imageid'];
		$imagepath=$this->db->escape(@$data['imagepath']);
		$field=array(

						'masanpham',
						'tensanpham',
						'mavach',
						'giacodinh',
						'sosanphamtrenlot',
						'dongiabancai',
						'dongiabanhop',
						'dongiabanthung',
						'dongiabanlot',
						'soluongton',
						'donggoi',
						'khuvuc',
						'phancap',
						'hienhanh',
						'ghichu',
						'loai',
						'manhom',
						'madonvi',
						'trongluong',
						'makho',
						
						'imageid',
						'imagepath'
						);
						$value=array(

						$masanpham,
						$tensanpham,
						$mavach,
						$giacodinh,
						$sosanphamtrenlot,
						$dongiabancai,
						$dongiabanhop,
						$dongiabanthung,
						$dongiabanlot,
						$soluongton,
						$donggoi,
						$khuvuc,
						$phancap,
						$hienhanh,
						$ghichu,
						$loai,
						$manhom,
						$madonvi,
						$trongluong,
						$makho,
						
						$imageid,
						$imagepath
						);

						$where="id = '".$id."'";
						$this->db->updateData("qlksanpham",$field,$value,$where);
						$log['tablename'] = "qlksanpham";
						$log['data'] = $data;
						$this->user->writeLog(json_encode($log));
						$this->updateFileTemp($imageid);
						return true;
	}

	public function delete($id)
	{
		/*$where="id = '".$id."'";
		 $this->db->deleteData("qlksanpham",$where);*/
		$field=array(

						'trangthai'
						);
						$value=array(


						'deleted'
						);

						$where="id = '".$id."'";
						$this->db->updateData("qlksanpham",$field,$value,$where);
	}

	public function deletedatas($listid)
	{
		foreach($listid as $item)
		{
			$this->delete($item);
		}
	}
	//Dinh luong linh kien
	public function getDinhLuongLinhKien($malinhkien)
	{
		$where = "AND malinhkien = '".$malinhkien."'";
		return $this->getDinhLuongs($where);
	}

	//Dinh luong san pham
	public function getDinhLuong($masanpham)
	{
		$where = "AND masanpham = '".$masanpham."'";
		return $this->getDinhLuongs($where);
	}



	public function getDinhLuongs($where)
	{
		$sql = "Select `qlksanpham_dinhluong`.*
									from `qlksanpham_dinhluong` 
									where 1=1 ".$where." Order by id";

		$query = $this->db->query($sql);
		return $query->rows;
	}

	public function getSanPhamDinhLuong($id)
	{
		$sql = "Select `qlksanpham_dinhluong`.*
									from `qlksanpham_dinhluong` 
									where id ='".$id."' ";
		$query = $this->db->query($sql);
		return $query->row;
	}

	public function saveSanPhamDinhLuong($data)
	{
		$id=(int)@$data['id'];
		$masanpham=$this->db->escape(@$data['masanpham']);
		$malinhkien=$this->db->escape(@$data['malinhkien']);
		$soluong=$this->string->toNumber($this->db->escape(@$data['soluong']));

		$field=array(

						'masanpham',
						'malinhkien',
						'soluong'
						
						);
						$value=array(

						$masanpham,
						$malinhkien,
						$soluong

						);

						if($id == 0 )
						{
							$this->db->insertData("qlksanpham_dinhluong",$field,$value);
						}
						else
						{
							$where="id = '".$id."'";
							$this->db->updateData("qlksanpham_dinhluong",$field,$value,$where);
						}


	}

	public function deletedSanPhamDinhLuong($id)
	{
		$where="id = '".$id."'";
		$this->db->deleteData("qlksanpham_dinhluong",$where);
	}
	//Chi phi san pham
	public function getChiPhi($masanpham)
	{
		$sql = "Select `qlksanpham_chiphi`.*
									from `qlksanpham_chiphi` 
									where masanpham = '".$masanpham."' Order by id";

		$query = $this->db->query($sql);
		return $query->rows;
	}

	public function getSanPhamChiPhi($id)
	{
		$sql = "Select `qlksanpham_chiphi`.*
									from `qlksanpham_chiphi` 
									where id ='".$id."' ";
		$query = $this->db->query($sql);
		return $query->row;
	}

	public function saveSanPhamChiPhi($data)
	{
		$id=(int)@$data['id'];
		$masanpham=$this->db->escape(@$data['masanpham']);
		$machiphi=$this->db->escape(@$data['machiphi']);
		$chiphi=$this->string->toNumber($this->db->escape(@$data['chiphi']));

		$field=array(

						'masanpham',
						'machiphi',
						'chiphi'
						
						);
						$value=array(

						$masanpham,
						$machiphi,
						$chiphi

						);

						if($id == 0 )
						{
							$this->db->insertData("qlksanpham_chiphi",$field,$value);
						}
						else
						{
							$where="id = '".$id."'";
							$this->db->updateData("qlksanpham_chiphi",$field,$value,$where);
						}


	}

	public function deletedSanPhamChiPhi($id)
	{
		$where="id = '".$id."'";
		$this->db->deleteData("qlksanpham_chiphi",$where);
	}
}
?>